//
//  BaseRequestTool.swift
//  BaseProject
//
//  Created by guolinsong on 2018/7/5.
//  Copyright © 2018年 guolin song. All rights reserved.
//

import UIKit
import Foundation
import Alamofire

import HandyJSON


// MARK:----------------------这个是接口请求--------------------
class BaseRequestTool: NSObject {


    
    //@escaping 逃逸闭包 ，函数执行之后才会执行
    
    class func loadHomeTitlesData(success:@escaping (_ topTitles:[TopTitleModel])->(),failure:@escaping (_ error:Error)->()) -> Void {
        

        BaseNetWork.RequestWith(Method:.get, URL: URL_topTitle, Parameter: nil, success: { (respones) in
           let dic:NSDictionary = respones as! NSDictionary
            let dic1:NSDictionary = dic.object(forKey: "data") as! NSDictionary
            //方法一
//            let Arr:NSArray = dic1.object(forKey: "data") as! NSArray
//            guard let cats = [TopTitleModel].deserialize(from: Arr) else  { return }


            //方法二
            let Arr:Array = dic1.object(forKey: "data")  as! Array<Any?>
            let json  = String.convertArrayToString(arr: Arr as [AnyObject])
//            print(dic1)
             guard let cats = [TopTitleModel].deserialize(from: json) else  { return }

            //遍历输出
            cats.forEach { (TopTitleModel) in
                print(TopTitleModel!.name!)
            }
            //成功回调
            success(cats as! [TopTitleModel])

        }) { (error) in

        
            print(error)
            //失败回调
            failure(error)

        }
       
        
        
//        //这种是单个模型中有 数组模型
//        let url = "http://capi.douyucdn.cn/api/v1/getbigDataRoom?aid=ios&client_sys=ios&time=1468636740&token=30890623_1b036814902f6451&auth=7d7026a323e09dd55c71ca215fc9d4b2"
//
//        BaseNetWork.RequestWith(Method:.get, URL: url, Parameter: nil, success: { (respones) in
//            let dic:NSDictionary = respones as! NSDictionary
//
////            responseModel.deserialize(from: dic)
////            JSONDeserializer<responseModel>.deserializeFrom(dict: dic)
//            
//            //这种是单个模型中有 数组模型
//            if let responseModel = responseModel.deserialize(from: dic) {
//
//                /// model转json 为了方便在控制台查看
//                print(responseModel.toJSONString(prettyPrint: true)!)
//
//                /// 遍历responseModel.data
//                responseModel.data?.forEach({ (model) in
//                    print(model.nickname!);
//                })
//            }
//
//        }) { (error) in
//
//            print(error)
//
//        }
    }

    class func loadHomeListData(success:@escaping (_ topTitles:[HomeListModel])->(),failure:@escaping (_ error:Error)->()) -> Void {
        BaseNetWork.RequestWith(Method: .get, URL: URL_categaryNews, Parameter: nil, success: { (respones) in
            let json = NewsJsonModel.deserialize(from: respones.string)
            var cats = [HomeListModel]()
            
            for dict in (json?.data)! {
                let someDict:NSDictionary = dict as! NSDictionary
                let someString = someDict.object(forKey: "content")
                cats.append(HomeListModel.deserialize(from: someString?))
            }
            print(cats)
            
        }) { (error) in
            print(error)
            failure(error)
        }
    }
    
}
